/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCheckIn.java
 *
 * Created on 16-03-2011, 18:47:22
 */

package GUI;

import interact.DataInteraction;
import interact.ValidateData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author UnderGround
 */
public class frmCheckIn extends javax.swing.JDialog {
   private int BookId;
   BorrowManagment borrow;
    /** Creates new form frmCheckIn */
    public frmCheckIn(java.awt.Frame parent, boolean modal, int id) {
        super(parent, modal);
        initComponents();
        Calendar cal = Calendar.getInstance();
        Date currentday = cal.getTime();
        txtReturnDate.setDate(currentday);
// DataInteraction.Employee_checkIn(tblBookInfo, 1);
        loadTable(2);
    }

    frmCheckIn(frmSearch aThis, boolean b) {
        initComponents();
//        this.BookId = BookID;
//        connect.loadDataToTableListBorrower("getBookById", tblBookInfo, BookID);
        Calendar cal = Calendar.getInstance();
        Date currentday = cal.getTime();
        txtReturnDate.setDate(currentday);
//          loadTable(1);
    }


    frmCheckIn(BorrowManagment aThis, boolean b,int emp_id) {
        initComponents();
        
        DataInteraction.Employee_checkIn(tblBookInfo, emp_id);
        Calendar cal = Calendar.getInstance();
        Date currentday = cal.getTime();
        txtReturnDate.setDate(currentday);


    }
    public static void loadTable(int emp_id){
         
//          DataInteraction.Employee_checkIn(tblBookInfo, emp_id);
        Calendar cal = Calendar.getInstance();
        Date currentday = cal.getTime();
        txtReturnDate.setDate(currentday);
        DataInteraction.Employee_checkIn(tblBookInfo, emp_id);
    }

   


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBookInfo = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jLabel5 = new javax.swing.JLabel();
        txtIssueDate = new com.toedter.calendar.JDateChooser();
        txtDueDate = new com.toedter.calendar.JDateChooser();
        txtReturnDate = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtFine = new javax.swing.JTextField();
        btnCaculator = new javax.swing.JButton();
        btnReturn = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        select = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jDesktopPane1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setText("ID");
        jLabel1.setBounds(140, 100, 30, 14);
        jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Name");
        jLabel2.setBounds(140, 130, 50, 14);
        jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtID.setEditable(false);
        txtID.setBounds(230, 90, 190, 20);
        jDesktopPane1.add(txtID, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtName.setEditable(false);
        txtName.setBounds(230, 130, 190, 20);
        jDesktopPane1.add(txtName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Issue Date");
        jLabel3.setBounds(140, 170, 70, 14);
        jDesktopPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Due Date");
        jLabel4.setBounds(140, 220, 70, 14);
        jDesktopPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tblBookInfo.setBackground(new java.awt.Color(255, 255, 204));
        tblBookInfo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblBookInfo);

        jScrollPane1.setBounds(0, 310, 640, 120);
        jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Return date");
        jLabel5.setBounds(140, 260, 90, 14);
        jDesktopPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtIssueDate.setEnabled(false);
        txtIssueDate.setBounds(230, 170, 190, 20);
        jDesktopPane1.add(txtIssueDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtDueDate.setEnabled(false);
        txtDueDate.setBounds(230, 220, 190, 20);
        jDesktopPane1.add(txtDueDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtReturnDate.setEnabled(false);
        txtReturnDate.setBounds(230, 260, 190, 20);
        jDesktopPane1.add(txtReturnDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel1.setBackground(new java.awt.Color(255, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel6.setText("Fine");

        btnCaculator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/calculator.png"))); // NOI18N
        btnCaculator.setText("Calculator");
        btnCaculator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCaculatorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtFine, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCaculator)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtFine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCaculator))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBounds(0, 440, 220, 80);
        jDesktopPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnReturn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/go-back.png"))); // NOI18N
        btnReturn.setText("Return book");
        btnReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReturnActionPerformed(evt);
            }
        });
        btnReturn.setBounds(353, 480, 120, 25);
        jDesktopPane1.add(btnReturn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/del.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        btnCancel.setBounds(490, 480, 90, 25);
        jDesktopPane1.add(btnCancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        select.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/select.png"))); // NOI18N
        select.setText("select ");
        select.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectActionPerformed(evt);
            }
        });
        select.setBounds(430, 90, 100, 25);
        jDesktopPane1.add(select, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel2.setBackground(new java.awt.Color(232, 249, 198));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), "Borrower infomation"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 458, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 254, Short.MAX_VALUE)
        );

        jPanel2.setBounds(80, 20, 470, 280);
        jDesktopPane1.add(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-656)/2, (screenSize.height-585)/2, 656, 585);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReturnActionPerformed
        // TODO add your handling code here:

        Vector temp = new Vector();
        String fine = txtFine.getText().trim();
        String id =txtID.getText().trim();
        if(id.trim().equals("")){
            ValidateData.showMessage("Please select a borrow");
            return;
        }
         if(fine.trim().equals("")){
             ValidateData.showMessage("Please calculate the fine");
             return;
         }
         else{
        String ReturnDate = ValidateData.convertDateToString(txtReturnDate.getDate());
        temp.add(id);
        temp.add(fine);
        temp.add(ReturnDate);
        temp.add(BookId);
        DataInteraction.updateData("testReturn",temp);//  ReturnBook
        ValidateData.showMessage("return book successful");
        dispose();
        }
    }//GEN-LAST:event_btnReturnActionPerformed

    private void btnCaculatorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCaculatorActionPerformed
        if(txtID.getText().trim().equals("")){
            ValidateData.showMessage("Please select a employee");
        }else{
            CheckFine();
        }
        
    }//GEN-LAST:event_btnCaculatorActionPerformed

    private void selectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectActionPerformed
     frmListBorrower listBorrower = new frmListBorrower(this, true);
     listBorrower.setModal(true);
        listBorrower.setVisible(true);
    }//GEN-LAST:event_selectActionPerformed
 public void CheckFine() {
        SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
        Calendar cal = Calendar.getInstance();
        String n = ValidateData.convertDateToString2(txtDueDate.getDate());
        Date txtDate;
     try {
            txtDate = f.parse(n.trim());
            long l = cal.getTime().getTime() - txtDate.getTime();

            if (cal.getTime().getTime() < txtDate.getTime()) {
                txtFine.setText("0");

            } else {
                int i = (int) ((l / (1000 * 60 * 60 * 24)) * 10);
                txtFine.setText(Integer.toString(i));
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error Check Fine!", "Eror's Message", JOptionPane.ERROR_MESSAGE);
            return;
        }

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmCheckIn dialog = new frmCheckIn(new javax.swing.JFrame(), true,1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCaculator;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReturn;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    protected static javax.swing.JButton select;
    protected static javax.swing.JTable tblBookInfo;
    protected static com.toedter.calendar.JDateChooser txtDueDate;
    private javax.swing.JTextField txtFine;
    protected static javax.swing.JTextField txtID;
    protected static com.toedter.calendar.JDateChooser txtIssueDate;
    protected static javax.swing.JTextField txtName;
    protected static com.toedter.calendar.JDateChooser txtReturnDate;
    // End of variables declaration//GEN-END:variables

}
